package com.twu.bibilioteca;

import com.sun.tools.corba.se.idl.InvalidArgument;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dixithk
 * Date: 6/12/14
 * Time: 2:31 PM
 * To change this template use File | Settings | File Templates.
 */
public class Application {




    public static  void main(String[] args) throws InvalidArgument {
        UserInterface ui = new UserInterfaceApp();
        Menu menu = new Menu();
        Library lib = new Library();
        PresentUsers registeredUsers = new PresentUsers();
        lib.loadBooks();
        lib.loadMovies();
        lib.loadUsers();




        
        
        
        



        ui.display("Welcome To Digital Library System");


        while(true)
        {


            List<MenuOperations> availableOptions = new ArrayList<MenuOperations>();
             availableOptions.clear();
            availableOptions .add(new ListAllBooks(ui, lib));
            availableOptions.add(new ListAllMovies(ui , lib));


            if(registeredUsers.userLoggedIn())
            {
                availableOptions .add(new CheckOutBook(ui, lib));
                availableOptions .add(new ReturnBook(ui, lib));

                availableOptions.add(new CheckOutMovie(ui, lib));
                availableOptions .add(new ReturnMovie(ui, lib));
                availableOptions.add(new LogOut(ui , lib));
            }
            else
            {
                availableOptions.add(new Login(ui , lib));
            }

            availableOptions .add(new MenuOperations() {


                @Override
                public Result action() {
                    return new Result("quit", true);  //To change body of implemented methods use File | Settings | File Templates.
                }

                @Override
                public String getName() {
                    return "quit";  //To change body of implemented methods use File | Settings | File Templates.
                }
            });

            List<String> menuOptions= menu.setOptions(availableOptions );
             ui.displayMenuOptions(menuOptions);

            try
            {
                int choice = Integer.parseInt(ui.takeUserChoice()) ;
                menu.isValidOption(choice);
                Result c =(availableOptions .get(choice - 1)).action() ;
                ui.printConclusion(c);
                if(c.getAnswer().equals("quit"))
                {
                    break;
                }
            }
            catch(NumberFormatException e)
            {
                ui.display("Invalid Option\n");
            }





        }

    }




}
